/*
 * messageBox 1.1.2
 * 	2009/09/16 beta
 * 	2009/11/11 
 * 		新增fontcolor可以设置字体颜色
 * 		新增延迟加载
 */

(function($){
	
	$.fn.mBox = function(options){
		
		var _settings = {
			type:'fadeout',
			title:'提示信息',
			message:'信息内容',
			fontcolor:'',
			value:'',
			shadow:true,//阴影
			alerttype:'warning',
			prompttype:'input',//input,textarea
			timeout:2000,
			times:5,
			width:0,
			draggable: true,//是否允许拖拽
			callback:''
		};
		_settings = $.extend(_settings,options);
	
		var _this = this;
		var _private = {
			repositionOnResize:true,//是否允许重新定位messagebox
			overlayOpacity: .01,
			overlayColor: '#FFF'
		};
		var doc = ((_this[0]==undefined)||!_this[0].id)?window:_this[0].id;
		var messagebox_outerWidth = 0;
		var messagebox_outerHeight = 0;
		var callback = _settings.callback;
		var userAgent = navigator.userAgent.toLowerCase(); 
		jQuery.browser.version = (userAgent.match( /.(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1];

		var ltie6 = $.browser.msie && parseInt($.browser.version) <= 6;

		initialize = function() {
			
			//if($('#messagebox')[0]) return false;
			_hide();
			if(_settings.type == 'fadeout'){
				$('body').append(
					'<div id="messagebox" class="messagebox-fadeout">'+
						'<div class="messagebox-fadeout-middle" style="color:'+_settings.fontcolor+'">'+_settings.message+'</div>'+
					    '<div class="messagebox-fadeout-right"></div>'+
					'</div>');
			}else{
				_overlay('show');
				
				$('body').append(
				  '<div id="messagebox" class="messagebox">'+
						'<h2 class="messagebox-title">'+_settings.title+'<span></span><a id="messagebox-close" href="#"></a></h2>'+
				    '<div class="messagebox-body">'+
				    	'<div id="messagebox-middle" class="messagebox-right body-style"></div>'+
				    '</div>'+
				    '<div class="messagebox-button"><div id="messagebox-under" class="messagebox-right"></div></div>'+
				    '<div class="messagebox-foot"><span></span></div>'+
				'</div>');
			}

			// IE6 Fix
			var pos = ltie6||doc!=window ? 'absolute' : 'fixed'; 
						
			$('#messagebox').css({
				position: pos,
				zIndex: 99999,
				padding: 0,
				margin: 0
			});
			
			messagebox_outerWidth = _settings.width!=0?_settings.width:$('#messagebox').outerWidth();
			
			$('#messagebox').css({
				minWidth: messagebox_outerWidth,
				maxWidth: messagebox_outerWidth,
				width:messagebox_outerWidth + 'px'
			});
			
			_reposition();
			_maintainPosition(true);
			
			switch( _settings.type ) {
				case 'alert':
					$('#messagebox-middle').append('<div class="messagebox-'+_settings.alerttype+'"></div><p>'+_settings.message+'</p>');
					$('#messagebox-under').append('<input type="button" value="确定" id="messagebox-yes" />');
					$('#messagebox-yes').click(function(){
						_hide();
						if( callback )callback(true);
					});
					$('#messagebox-close').click(function(){
						_hide();
					});
					break;
				case 'confirm':
					$('#messagebox-middle').append('<div class="messagebox-question"></div><p>'+_settings.message+'</p>');
					$('#messagebox-under').append('<input type="button" value="确定" id="messagebox-yes" /><input type="button" value="取消" id="messagebox-no" />');
					$('#messagebox-yes').click( function(){
						_hide();
						if( callback ) callback(true);
					});
					$('#messagebox-no,#messagebox-close').click( function() {
						_hide();
						if( callback ) callback(false);
					});
					break;
				case 'prompt':
					var contentHtml = _settings.prompttype == 'textarea'?'<textarea id="message-prompt-text" class="message-prompt-text" >'+_settings.value+'</textarea>':'<input type="text" id="message-prompt-text" class="message-prompt-text" value="'+_settings.value+'" />';
					$('#messagebox-middle').append('<div class="messagebox-question"></div><h3>'+_settings.message+'：</h3>'+ contentHtml);
					$('#messagebox-under').append('<input type="button" value="确定" id="messagebox-yes" /><input type="button" value="取消" id="messagebox-no" />');
					var message_prompt_text = $('#message-prompt-text');
					if( _settings.value ){
						message_prompt_text.val(_settings.value);
						message_prompt_text.focus().select();
					}
					$('#messagebox-yes').click( function() {
						var val = message_prompt_text.val();
						_hide();
						if( callback ) callback( val );
					});
					$('#messagebox-no,#messagebox-close').click( function() {
						_hide();
					});
					break;
				case 'fadeout':
						setTimeout(function() {
							$('.messagebox-fadeout').fadeOut('slow', function() {
										$(this).remove();
									});
						}, _settings.timeout);
					break;
				case 'progress':
					var count = 1;
					var times_count = 1;
					$('#messagebox-middle').append('<div class="messagebox"></div><h3>'+_settings.message+'：</h3><div id="message-progress" class="message-progress"><div id="message-progress-loading" class="message-progress-loading"><div><div>');
					var doProgress = function(){
						if(_settings.times*5 >= times_count){
							setTimeout(function(){
								var temp = (count++)%10;
								temp = temp==0?10:temp;
								$('#message-progress-loading').width(temp*10+'%');
								doProgress();
							},300);
							times_count++;
						}else{
							_hide();
						}
					}
					doProgress();
					break;
			}
			
			if(_settings.type !='fadeout'){
				$('#messagebox-yes').focus();
				$('#messagebox-yes,#messagebox-no,#messagebox-close').keypress( function(e) {
					if( e.keyCode == 13 ) $(this).trigger('click');
					if( e.keyCode == 27 ) $('#messagebox-close').trigger('click');
				});
				_over_down();
			}
				
			//拖拽
			if( _settings.draggable ) {
				try {
					$('#messagebox').draggable({ handle: $('#messagebox-title') });
					$('#messagebox-title').css({ cursor: 'move' });
				} catch(e) { /* requires jQuery UI draggables */ }
			}
		}
				
		//隐藏
		_hide = function(){
			$('#messagebox').remove();
			_overlay('hide');
			_maintainPosition(false);
		}
		
		//定位
		_reposition = function() {
			
			var top = 0;
			var left = 0;
			
			if(messagebox_outerHeight == 0) messagebox_outerHeight = $('#messagebox').outerHeight();
			
			if(doc == window){
				top = (($(window).height() / 2) - (messagebox_outerHeight / 2)) - 40;
				left = (($(window).width() / 2) - (messagebox_outerWidth / 2));
			}else{
				var doc_outerHeight = $('#'+doc).outerHeight();
				var doc_outerWidth = $('#'+doc).outerWidth();
				if(messagebox_outerHeight > doc_outerHeight){
					top = $('#'+doc).offset().top - (messagebox_outerHeight - doc_outerHeight)/2 - 40;
				}else{
					top = $('#'+doc).offset().top + (doc_outerHeight - messagebox_outerHeight)/2 - 40;
				}
				if(messagebox_outerWidth > doc_outerWidth){
					left = $('#'+doc).offset().left - (messagebox_outerWidth - doc_outerWidth)/2;
				}else{
					left = $('#'+doc).offset().left + (doc_outerWidth - messagebox_outerWidth)/2;
				}
			}
			
			if( top < 0 ) top = 0;
			if( left < 0 ) left = 0;
			
			if(ltie6 && doc == window) top = top + $(window).scrollTop();
				
			$('#messagebox').css({
				top: top + 'px',
				left: left + 'px'
			});
	
			$("#messageBox-overlay").height( $(document).height() );
		}
		
		//背景覆盖层
		_overlay = function(status) {
			switch( status ) {
				case 'show':_hide();
					//if($('#messageBox-overlay')[0]) return false;
					$('body').append('<div id="messageBox-overlay"></div>');
					$('#messageBox-overlay').css({
						position: 'absolute',
						zIndex: 99998,
						top: '0px',
						left: '0px',
						width: '100%',
						height: $(document).height(),
						background: _private.overlayColor,
						opacity: _private.overlayOpacity
					});
					break;
				case 'hide':
					$('#messageBox-overlay').remove();
					break;
			}
		}
		
		//鼠标滑过
	_over_down = function(){
		$('#messagebox-under').find(':button').hover(
			function(){$(this).addClass('over')},
			function(){$(this).removeClass('over')}
		).mousedown(
			function(){
				$(this).addClass('down')
			}
		);
	}
		
		//改变窗口大小时候定位
		_maintainPosition = function(status) {
			if(_private.repositionOnResize) {
				switch(status) {
					case true:
						$(window).bind('resize', function() {
							_reposition();
						});
					break;
					case false:
						$(window).unbind('resize');
					break;
				}
			}
		}
		setTimeout(initialize,'100');
	}
	
})(jQuery);
